import { Component, OnInit } from '@angular/core';
import { NzModalRef, NzMessageService } from 'ng-zorro-antd';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { mobileValidate } from '@core/util/formControl-validate-utils';
import { SalesmanService } from '@core/routes-services/salesman.service';
import { DistrictService } from '@core/routes-services/district.service';
import { MarketFansService } from '@core/routes-services/market-fans.service';
import { MARKET_SALESMAN_ROLE } from '../../../constant/dictionary';

@Component({
  selector: 'app-market-fans-salesman-edit',
  templateUrl: './edit.component.html',
})
export class FansSalesmanEditComponent implements OnInit {
  customerId: any = 0;
  entity: any = {};
  validateForm: FormGroup;
  isLoading: boolean = false;

  districtOption: any [] = [];

  marketSalesmanRole: any[] = MARKET_SALESMAN_ROLE;

  constructor(private fb: FormBuilder,
              private modal: NzModalRef,
              private marketFansService: MarketFansService,
              private districtService: DistrictService,
              private salesmanService: SalesmanService,
              public message: NzMessageService) {
  }

  ngOnInit(): void {
    this.initDistrice();
    this.initValidateForm();
    this.initEntity();
  }

  initDistrice() {
    this.districtService.getAll().then((res) => {
      if (res.status.toString().startsWith('20')) {
        this.districtOption = res.data;
      } else {
        this.message.warning(res.message);
      }
      this.isLoading = false;
    }, error => {
      this.message.error(error.message ? error.message : error.error);
      this.isLoading = false;
    });
  }

  initEntity() {
    if (this.customerId != 0) {
      this.marketFansService.generateSalesman(this.customerId).then((res) => {
        console.log(res);
        if (res.status.toString().startsWith('20')) {
          this.entity = res.data;
          let maxLevel = this.entity.role ? this.entity.role : 4;
          this.marketSalesmanRole = this.marketSalesmanRole.filter((t) => t.key <= maxLevel);
          this.initValidateForm();
        } else {
          this.message.warning(res.message);
        }
        this.isLoading = false;
      }, error => {
        this.message.error(error.message ? error.message : error.error);
        this.isLoading = false;
      });
    }
  }

  initValidateForm() {
    //角色: 1-业务员，2-渠道商，3-门店，4-代理
    this.validateForm = this.fb.group({
      name: [this.entity.name, [Validators.required, Validators.maxLength(30)]],
      phone: [this.entity.phone, [Validators.required, mobileValidate]],
      idNumber: [this.entity.idNumber, []],
      districtId: [this.entity.districtId, [Validators.required]],
      role: [this.entity.role, [Validators.required]],
      parentId: [this.entity.parentId, []],
    });

  }

  submitForm(): void {
    if (this.validateForm.invalid) {
      for (const i in this.validateForm.controls) {
        this.validateForm.controls[i].markAsDirty();
        this.validateForm.controls[i].updateValueAndValidity();
      }
    } else {
      this.isLoading = true;
      this.salesmanService.generateOrUPdateEntity(this.entity.id, this.validateForm.value).then((res) => {
        if (res.status.toString().startsWith('20')) {
          this.modal.close(true);
        } else {
          this.message.warning(res.message);
        }
        this.isLoading = false;
      }, error => {
        this.message.error(error.message ? error.message : error.error);
        this.isLoading = false;
      });
    }
  }

  close() {
    this.modal.destroy();
  }
}
